package com.example.practice4.controller;

import com.example.practice4.Entity.Register;
import com.example.practice4.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("register")
public class RegisterController {
    @Autowired
    private UserRepository userRepository;
    @Autowired
    private PasswordEncoder passwordEncoder;

    @GetMapping
    public String displayRegisterForm(){
        return "registerForm";
    }

    @PostMapping
    public String register(Register register){
        userRepository.save(register.toUser(passwordEncoder));
        return "redirect:/login";
    }
}
